package guoanmaker.personal.business.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.personal.business.model.PersonalAppraiseStates;
import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.service.PersonalAppraiseService;
import guoanmaker.personal.helper.common.AjaxJson;
import guoanmaker.personal.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.Jedis;
/**
 * 
* @ClassName: PersonalAppraiseController 
* @Description: 评价controller
* @author Yvonne_MGL
* @date 2017年7月20日 下午3:13:53 
*  
 */
@RestController
@RequestMapping(ApiController.APPRAISE_URL)
public class PersonalAppraiseController {
	@Resource
	private PersonalAppraiseService personalAppraiseService;
	
	@Resource
	private AjaxJson ajaxJson;
	
	@Autowired  
	private  HttpServletRequest request;
	
	/**
	 * 
	* @Description: 查询isused=1的评价选项
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月20日 下午3:15:11     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询正在使用的评价选项", notes = "查询正在使用的评价选项")
	@RequestMapping(value = "/findAppraiseStatesIsUsed", method = RequestMethod.POST)
	public AjaxJson findAppraiseStatesIsUsed(){
		List<PersonalAppraiseStates> list=new ArrayList<>();
		try {
			list=personalAppraiseService.findAppraiseStatesIsUsed();
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(list);
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(list);
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 保存订单评价信息 
	* @param orderid 订单id
	* @param info 评价分数及类别，格式为  评价类别名称@分数,评价类别名称@分数,评价类别名称@分数,...
	* @param appraise 评语
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月20日 下午5:07:44     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "评价", notes = "评价")
	@RequestMapping(value = "/saveAppraise", method = RequestMethod.POST)
	public AjaxJson saveAppraise(String orderid,String info,String appraise){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalAppraiseService.saveAppraise(orderid, info, appraise);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		
		return ajaxJson;
	}

}
